﻿using System;
using Beaker;
using Beaker.Injection;
using Beaker.Specs;
using OpenCube.IO;

namespace OpenCube.Nbt.Specs.Builders
{
	[CLSCompliant(false)]
	public sealed class NbtDeserializerBuilder
		: Builder<NbtDeserializer>
	{
		private Maybe<IBinaryReaderFactory> _readerFactory;
		private Maybe<IFactory<INbtDictionary>> _dictionaryFactory;

		public NbtDeserializerBuilder WithReaderFactory(IBinaryReaderFactory readerFactory)
		{
			_readerFactory = new Maybe<IBinaryReaderFactory>(readerFactory);
			return this;
		}

		public NbtDeserializerBuilder WithDictionaryFactory(IFactory<INbtDictionary> dictionaryFactory)
		{
			_dictionaryFactory = new Maybe<IFactory<INbtDictionary>>(dictionaryFactory);
			return this;
		}

		public override NbtDeserializer Build()
		{
			IBinaryReaderFactory readerFactory = _readerFactory.GetValueOrDefault(new BinaryReaderFactory());
			IFactory<INbtDictionary> dictionaryFactory = _dictionaryFactory.GetValueOrDefault(new FactoryMockBuilder<INbtDictionary>().WithCreateInstance(() => new NbtDictionary()).Build);

			return new NbtDeserializer(readerFactory, dictionaryFactory);
		}
	}
}